package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.search.activity.SearchHomePageActivity;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 * 收缩版adapter
 */
public class SearchVpGoodsRvAdapter extends RecyclerView.Adapter<SearchVpGoodsRvAdapter.SearchPeiXunRvViewHolder>{

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> list;
    private SearchHomePageGoodsListener listener;

    public SearchHomePageGoodsListener getListener() {
        return listener;
    }

    public void setListener(SearchHomePageGoodsListener listener) {
        this.listener = listener;
    }

    private static int itemCount = 3;

    public void addItemCount(int num) {
        itemCount = num;
    }

    public SearchVpGoodsRvAdapter(Context context, List<GoodsSortSearchAllBzBean.DataBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public SearchPeiXunRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_goods_sort_ts_rv,viewGroup,false);
        return new SearchPeiXunRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchPeiXunRvViewHolder holder, int i) {

        //设置标题筛选高亮
        if (list != null){
            SpannableString spannableString = new SpannableString(list.get(i).getTitle());
            int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
            if (startIndex>=0){
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                spannableString.setSpan(colorSpan, startIndex, startIndex+SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                holder.title.setText(spannableString);
            }else {
                holder.title.setText(list.get(i).getTitle());
            }
        }

        Glide.with(context).load(list.get(i).getImg()).placeholder(R.mipmap.doc_default).into(holder.img);
        holder.desc.setText(list.get(i).getDesc());
        holder.price.setText("¥" + list.get(i).getPrice() + "");
        holder.car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = list.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx","splite:"+split[2]);
                String skuStr = "{"+"\""+split[2].toString()+"\""+":"+"1}";
                listener.searchHomePageGoodsAddCar(skuStr);
            }
        });
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = list.get(i).getUrl();
                String[] split = s.split("/");
                Log.e("cjx", "URL：" + split[2]);
                context.startActivity(DetailProductActivity.getIntent(context, split[2]));
            }
        });
    }

    @Override
    public int getItemCount() {
//        if (list.size() > 3)
//            return itemCount;
//        else
            return list.size();
    }

    class SearchPeiXunRvViewHolder extends RecyclerView.ViewHolder{
        TextView title, desc, price;
        ImageView car, img;
        LinearLayout linearLayout;
        public SearchPeiXunRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.goods_sort_ts_rv_img);
            price = itemView.findViewById(R.id.goods_sort_ts_rv_price);
            title = itemView.findViewById(R.id.goods_sort_ts_rv_title);
            car = itemView.findViewById(R.id.goods_sort_ts_rv_car);
            desc = itemView.findViewById(R.id.goods_sort_ts_rv_desc);
            linearLayout = itemView.findViewById(R.id.goods_sort_ts_LinearLayout);
        }
    }
    public interface SearchHomePageGoodsListener {
        void searchHomePageGoodsAddCar(String skuStr);
    }
}
